﻿namespace DeltaGroup.WheelOfJeopardy.UI.ViewModel
{
    using System.Collections.ObjectModel;
    using System.Linq;

    using DeltaGroup.WheelOfJeopardy.Model;
    using System.ComponentModel;
    using System;

    public class CategoryModel : BaseViewModel, IDataErrorInfo
    {
        #region Fields

        private string _title;

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CategoryModel"/> class.
        /// </summary>
        /// <param name="basePointMultiple">The base point multiple.</param>
        public CategoryModel(int basePointMultiple)
        {
            Challenges = new ObservableCollection<ChallengeModel>();
            for (int j = 0; j < 5; j++)
            {
                Challenges.Add(new ChallengeModel() { Index = j + 1, Point = basePointMultiple * (j + 1) });
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CategoryModel"/> class.
        /// </summary>
        /// <param name="category">The category.</param>
        public CategoryModel(Category category, int basePointMultiple)
        {
            Challenges = new ObservableCollection<ChallengeModel>(category.Challenges.Select((c, i) => new ChallengeModel(c, i + 1) { Category = category.Title, Point = basePointMultiple * (i + 1) }));
            _title = category.Title;
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Gets or sets the challenges.
        /// </summary>
        /// <value>The challenges.</value>
        public ObservableCollection<ChallengeModel> Challenges
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>The title.</value>
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                FirePropertyChanged("Title");
            }
        }

        public string Error
        {
            get { return null; }
        }

        #endregion Properties

        #region IDataErrorInfo Members

        public string this[string columnName]
        {
            get
            {
                if (columnName == "Title")
                {
                    if (String.IsNullOrWhiteSpace(Title))
                    {
                        return "Please provide a name for the category of challenges";
                    }
                }
                return null;
            }
        }

        #endregion
    }
}